Next: Declare Form, Previous: Obsolete Functions, Up: Functions [Contents][Index]
An inline function is a function that works just like
an ordinary function, except for one thing: when you byte-compile
a call to the function (see Byte Compilation),
the function’s definition is expanded into the caller. To
define an inline function, use defsubst instead of
defun.
This macro defines an inline function. Its syntax is
exactly the same as defun (see Defining
Functions).
Making a function inline often makes its function calls run faster. But it also has disadvantages. For one thing, it reduces flexibility; if you change the definition of the function, calls already inlined still use the old definition until you recompile them.
Another disadvantage is that making a large function inline can increase the size of compiled code both in files and in memory. Since the speed advantage of inline functions is greatest for small functions, you generally should not make large functions inline.
Also, inline functions do not behave well with respect to
debugging, tracing, and advising (see Advising
Functions). Since ease of debugging and the flexibility of
redefining functions are important features of Emacs, you should
not make a function inline, even if it’s small, unless its
speed is really crucial, and you’ve timed the code to
verify that using defun actually has performance
problems.
After an inline function is defined, its inline expansion can be performed later on in the same file, just like macros.
It’s possible to use defsubst to define a
macro to expand into the same code that an inline function would
execute (see Macros). But the
macro would be limited to direct use in expressions—a macro
cannot be called with apply, mapcar and
so on. Also, it takes some work to convert an ordinary function
into a macro. To convert it into an inline function is easy; just
replace defun with defsubst. Since each
argument of an inline function is evaluated exactly once, you
needn’t worry about how many times the body uses the
arguments, as you do for macros.
As an alternative to defsubst, you can use
define-inline to define functions via their
exhaustive compiler macro. See define-inline.
Next: Declare Form, Previous: Obsolete Functions, Up: Functions [Contents][Index]